function nodesForEach(nodes, proc) {
  for (var i = 0, len = nodes.length; i < len; i++) proc(nodes[i]);
}

function checkB(aB) {
  var title = aB.textContent;
  switch (title) {
  case "特別企画":
  case "チャット":
  case "運営案内":
  case "ツール類":
  case "まちＢＢＳ":
  case "他のサイト":
    return false;
  }
  return true;
}

function listupAsUnderB(aB) {
  var arr = [];
  var cur = aB.nextSibling;
  while (cur && cur.tagName != "B") {
    if (cur.tagName == "A") {
      arr.push(cur);
    }
    cur = cur.nextSibling;
  }
  return arr;
}

function f1(aA) {
  return "<li class='sub unselectable collapsed unread'><a href='/ktkr.html?board=" + aA.getAttribute("href") + "' class='link bbsmenu'><span title='" + aA.textContent + "' class='name name-d-2'><span class='name-text name-text-d-2'>" + aA.textContent + "</span></span></a></li>";
}

function makeFolders() {
  var div = document.createElement("div");
  nodesForEach(document.getElementsByTagName("B"), function(x) {
    if (!checkB(x)) return;
    var li = document.createElement("li");
    li.setAttribute("class", "folder unselectable collapsed");
    li.setAttribute("onclick", "toggle_folder(this);");
    li.innerHTML = "<a class='link'><img alt='' src='images/tree-view-folder-closed.gif' class='icon icon-d-1' height='16' width='16'><span title=" + x.textContent + " class='name name-d-1 name-unread'><span class='name-text name-text-d-1'>" + x.textContent + "</span></span></a>";
    var ul = document.createElement("ul");
    ul.innerHTML = listupAsUnderB(x).map(f1).join("");
    li.appendChild(ul);
    div.appendChild(li);
  });
  document.body.appendChild(div);
}

function makeBoardsList() {
  var obj = {};
  nodesForEach(document.getElementsByTagName("B"), function(x) {
    if (checkB(x)) {
      listupAsUnderB(x).forEach(function(y) {
        obj[y.getAttribute("href")] = true;
      });
    }
  });
  var arr = [];
  for (var p in obj) {
    arr.push("&nbsp;&nbsp;\"" + p + "\"");
  }
  var div = document.createElement("div");
  div.innerHTML = arr.join(":True,<br\>") + ":True";
  document.body.appendChild(div);
}

function main() {
  makeFolders();
  makeBoardsList();
}

window.addEventListener("load", main, false);


